package org.goodsl.mdd.validator;

import java.util.regex.Matcher;

import org.goodsl.mdd.Seg;
import org.goodsl.mdd.SingleValidResult;
import org.goodsl.mdd.Validator;

/**
 * Created with IntelliJ IDEA.
 * User: Simon Xiao
 * Date: 12-11-13
 * Time: 下午4:21
 * To change this template use File | Settings | File Templates.
 */
public class PatternValidator extends Validator<Seg> {


    private java.util.regex.Pattern pattern;

    public PatternValidator(String patternStr, String message){
        this(patternStr, 0, message);

    }

    public PatternValidator(String patternStr, int flag, String message){
        super(message);
        pattern = java.util.regex.Pattern.compile( patternStr, flag );


    }
    @Override
    public SingleValidResult<Seg> verify(Seg variable) {
        String valueStr = variable.getValue().toString();
        Matcher m = pattern.matcher( valueStr );
        if(m.matches())
            return new SingleValidResult<Seg>(true,"",variable,this);
        else
            return new SingleValidResult<Seg>(false,getMessage(),variable,this);

    }
}
